package JavaConcurrentMei.一;

public class DeadLock {
    private static volatile Object resourceA = new Object();
    private static volatile Object resourceB = new Object();

    public static void main(String[] args) throws Exception{
        new Thread(() -> {
            synchronized (resourceA) {
                System.out.println(Thread.currentThread().getName() + " get resourceA");
                synchronized (resourceB) {
                    System.out.println(Thread.currentThread().getName() + " get resourceB");
                    System.out.println(Thread.currentThread().getName() + " release resourceA");
                    try {
                        resourceA.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t1").start();

        new Thread(() -> {
            // 睡眠1s，目的是让t1获取a和b
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (resourceA) {
                System.out.println(Thread.currentThread().getName() + " get resourceA");
                System.out.println(Thread.currentThread().getName() + " try get resourceB");
                synchronized (resourceB) {
                    System.out.println(Thread.currentThread().getName() + " get resourceB");
                    System.out.println(Thread.currentThread().getName() + " release resourceA");
                    try {
                        resourceA.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t2").start();


    }
}
